package com.vegetable.modules.controller.inbound;

import com.alibaba.fastjson.JSON;
import com.vegetable.common.annotation.PreventDuplicateSubmit;
import com.vegetable.common.annotation.SysLog;
import com.vegetable.common.utils.PageUtils;
import com.vegetable.common.utils.R;
import com.vegetable.modules.controller.AbstractController;
import com.vegetable.modules.entity.inbound.OtherDelivery;
import com.vegetable.modules.service.inbound.IOtherDeliveryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 其他入库表 前端控制器
 * </p>
 *
 * @author wangruijie
 * @since 2024-03-11
 */
@Slf4j
@RestController
@RequestMapping("/inStorage/otherDelivery")
@AllArgsConstructor
@Api(value = "其他入库", tags = "其他入库")
public class OtherDeliveryController extends AbstractController {

    private IOtherDeliveryService otherDeliveryService;
    /**
     * 所有其他入库列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "条件查询其他入库列表", notes = "条件查询其他入库列表")
    public R list(@RequestParam Map<String, Object> params){
        log.info("条件查询其他入库列表{}", JSON.toJSONString(params));
        PageUtils page = otherDeliveryService.queryPage(params);
        return R.data( page);
    }

    /**
     * 详情其他入库
     */
    @SysLog(value = "详情其他入库")
    @GetMapping("/detailOtherDelivery")
    @ApiOperation(value = "根据id查询详情其他入库", notes = "根据id查询详情其他入库")
    public R detailOtherDelivery(Long id){
        if (id == null || id == -1){
            return R.fail("订单主键不能为空");
        }
        log.info("根据id查询详情其他入库：{}", JSON.toJSONString(id));
        try {
            return otherDeliveryService.detailOtherDelivery(id);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("错误信息:{}",e.getLocalizedMessage());
            return R.fail(e.getMessage());
        }
    }

    /**
     * 新增其他入库
     */
    @SysLog(value = "新增其他入库")
    @PostMapping("/saveOtherDelivery")
    @ApiOperation(value = "新增其他入库", notes = "新增其他入库")
    public R saveOtherDelivery(@RequestBody OtherDelivery otherDelivery){
        log.info("新增其他入库：{}", JSON.toJSONString(otherDelivery));
        otherDelivery.setWorkerId(getUserId());
        otherDelivery.setWorkerName(getUser().getRealName());
        otherDelivery.setCreateUserId(getUserId());
        try {
            return otherDeliveryService.saveOtherDelivery(otherDelivery);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("错误信息:{}",e.getLocalizedMessage());
            return R.fail(e.getMessage());
        }
    }

    /**
     * 修改其他入库
     */
    @SysLog(value = "修改其他入库")
    @PostMapping("/updateOtherDelivery")
    @ApiOperation(value = "修改其他入库", notes = "修改其他入库")
    public R updateOtherDelivery(@RequestBody OtherDelivery otherDelivery){
        log.info("修改其他入库：{}", JSON.toJSONString(otherDelivery));
        if (otherDelivery.getId() == null || otherDelivery.getId() == -1){
            return R.fail("订单主键不能为空");
        }
        otherDelivery.setCreateUserId(getUserId());
        try {
            return otherDeliveryService.saveOtherDelivery(otherDelivery);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("错误信息:{}",e.getLocalizedMessage());
            return R.fail(e.getMessage());
        }
    }

    /**
     * 提交其他入库
     */
    @SysLog(value = "提交其他入库")
    @PostMapping("/submitOtherDelivery")
    @PreventDuplicateSubmit(expireTime = 5)
    @ApiOperation(value = "提交其他入库", notes = "修改其他入库")
    public R submitOtherDelivery(@RequestBody OtherDelivery otherDelivery){
        log.info("提交其他入库：{}", JSON.toJSONString(otherDelivery));
        otherDelivery.setCreateUserId(getUserId());
        try {
            return otherDeliveryService.submitOtherDelivery(otherDelivery);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("错误信息:{}",e.getLocalizedMessage());
            return R.fail(e.getMessage());
        }
    }

    /**
     * 删除其他入库
     */
    @SysLog(value = "删除其他入库")
    @GetMapping("/deleteOtherDelivery")
    @ApiOperation(value = "根据id删除其他入库", notes = "根据id删除其他入库")
    public R deleteOtherDelivery(Long id){
        log.info("删除其他入库：{}", JSON.toJSONString(id));
        try {
            return otherDeliveryService.deleteOtherDelivery(id);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("错误信息:{}",e.getLocalizedMessage());
            return R.fail(e.getMessage());
        }
    }
}
